<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客详情页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/detail.css">
    <link rel="stylesheet" href="css/pinglun.css">
    <style>
        .test {
            height: auto;
            margin: 0 15%;
        }
    </style>
</head>

<body>
    <div class="nav">
        <img src="pic/bokelogo.jpg" alt="">
        <span class="blog-title">小博欢迎您</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
        <a class="nav-span" href="login.html" onclick="logout()">注销</a>
    </div>
    <div class="container">
        <div class="left">
            <div class="card">
                <img src="pic/aoao.jpg" alt="">
                <h3></h3>
                <a href="#">Github</a>
                <div class="row">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="row2">
                    <span1></span1>
                    <span2>1</span2>
                </div>
            </div>
        </div>
        <div class="right">
            <div class="content">
                <input type="hidden" id="blogId" class="blogId">
                <div class="title"></div>
                <div class="date"></div>
                <div class="detail" id="detail"> </div>
                <div class="operating"></div>

                <br>
                <br>
                <div class="test">
                    <div lang="en-US" class="gitment-container gitment-editor-container">
                        <div class="gitment-editor-main">
                            <div class="gitment-editor-body">
                                <div class="gitment-editor-write-field">
                                    <textarea placeholder="你想说些什么" id="content" name="content"></textarea>
                                </div>
                            </div>
                        </div>
                        <div class="gitment-editor-footer">
                            <input type="button" class="gitment-editor-submit" value="评论" id="submit"
                                onclick="submit()">
                        </div>
                    </div>
                </div>
                <div class="right1"></div>
            </div>
        </div>

    </div>
    </div>

    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="blog-editormd/css/editormd.css" />
    <script src="js/jquery.min.js"></script>
    <script src="blog-editormd/lib/marked.min.js"></script>
    <script src="blog-editormd/lib/prettify.min.js"></script>
    <script src="blog-editormd/editormd.js"></script>
    <script src="js/common.js"></script>
    <script>
        function submit() {
            $.ajax({
                type: "post",
                url: "/comment/add" + location.search,
                data: {
                    content: $("#content").val(),
                },
                success: function (result) {
                    if (result.code == 200) {
                        location.href = "blog_detail.html"+location.search;
                    }
                    else {
                        alert(result.error);
                    }
                }
            });
        }
    </script>
    <script>
        $.ajax({
            type: "get",
            url: "/blog/getBlogDetail" + location.search,
            success: function (result) {
                if (result.code == 200 && result.data != null) {
                    var blog = result.data;
                    $(".blogId").text(blog.blogId);
                    $(".title").text(blog.title);
                    $(".date").text(blog.createTime);
                    // $(".detail").text(blog.content);
                    editormd.markdownToHTML('detail', { markdown: blog.content });
                    var finalHtml = "";
                    if (blog.loginUser) {
                        finalHtml += '<button onclick="window.location.href=\'blog_update.html?blogId=' + blog.id + '\'">编辑</button>';
                        finalHtml += '<button onclick="deleteBlog(' + blog.id + ')">删除</button>';
                    }
                    finalHtml += '<button onclick="upvoteBlog(' + blog.id + ')">点赞</button>' + '点赞数量：' + blog.upvote;
                    $(".operating").html(finalHtml);

                }
            }
        });
        function upvoteBlog(blogId) {
            $.ajax({
                type: "post",
                url: "/blog/upvote" + location.search,
                success: function (result) {
                    if (result.code == 200 && result.data == true) {
                        location.href = "blog_detail.html" + location.search;
                    }
                }
            });
        }
        function deleteBlog(blogId) {
            $.ajax({
                type: "post",
                url: "/blog/delete" + location.search,
                success: function (result) {
                    if (result.code == 200 && result.data == true) {
                        location.href = "blog_list.html";
                    }
                }
            });
        }
    </script>
    <script>
        function getAuthorInfo() {
            $.ajax({
                type: "get",
                url: "/user/getAuthorInfo" + location.search,
                success: function (result) {
                    console.log(result.data)
                    if (result != null)
                        if (result.code == 200 && result.data != null) {
                            var user = result.data;
                            $(".left .card h3").text(user.userName);
                            $(".left .card a").attr("href", user.githubUrl);
                            $(".left .card .row2 span1").text(user.atricals);
                        }
                        else {
                            location.href = "login.html";
                        }
                }
            });
        }
        getAuthorInfo();
    </script>
    <script>
        getBlogComment();
        function getBlogComment() {
            $.ajax({
                url: "/comment/getComments" + location.search,
                type: "get",
                success: function (result) {
                    if (result.code == 200 && result.data != null) {
                        var comments = result.data;
                        console.log(comments);
                        var finalHtml = "";
                        for (var comment of comments) {
                            finalHtml += '<div class="comment">';
                            finalHtml += '<div class="userId">' + comment.userName + '</div>';
                            finalHtml += '<div class="content">' + comment.content + '</div>';
                            finalHtml += '<div class="date">' + comment.createTime + '</div>';
                            finalHtml += '</div>';
                        }
                        $(".right1").html(finalHtml);
                    }
                    else {
                        console.log(result.data);
                    }
                },

            });
        }
    </script>
</body>

</html>